package controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import serice.impl.Userserivceimpl;
import entity.User;
import entity.ajaxResult;

@Controller
public class Usercontroller {
	@Autowired
	private Userserivceimpl service;
	
	@RequestMapping("register")
	@ResponseBody
	public ajaxResult register(User u){
		u.setUemail(u.getUname()+"@qq.com");
		u.setUheadPortrait("image/头像4.jpg");
		u.setBalance(1);
		u.setNickname(u.getNickname()+"同学");
		ajaxResult ajax=new ajaxResult();
		try {
			service.insertUser(u);
			ajax.setMsg("success");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ajax.setMsg("fail");
		}
		return ajax;
	}
	@RequestMapping("login")
	@ResponseBody
	public ajaxResult login(String username,String password,HttpSession session){
		User u=service.selectUser(username, password);
		ajaxResult ajax=new ajaxResult();
		if(u==null){
			ajax.setMsg("fail");
			session.invalidate();
		}else{
			ajax.setMsg("success");
			session.setAttribute("currentUser", u);
		}
		return ajax;
	}
}
